package cn.holy.app.sztxc.repository;

import android.arch.lifecycle.LiveData;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.concurrent.Executor;

import javax.inject.Inject;

import cn.holy.app.sztxc.app.App;
import cn.holy.app.sztxc.database.dao.UserDao;
import cn.holy.app.sztxc.domain.LoginFormBean;
import cn.holy.app.sztxc.domain.UserInfoBean;
import cn.holy.app.sztxc.domain.net.ApiResponse;
import cn.holy.app.sztxc.net.ApiClient;
import cn.holy.app.sztxc.net.callback.LoadingCallback;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by xionghao on 2018/2/22.
 */


public class UserRepository {


    private final UserDao userDao;

    @Inject
    public UserRepository(UserDao userDao) {
        this.userDao = userDao;
    }

    public void saveLoginInfo(String username, String password) {
        SharedPreferences sharedPreferences = App.getApp().getSharedPreferences("app_szt_xc", Context.MODE_PRIVATE);
        sharedPreferences.edit()
                .putString("username", username)
                .putString("password", password)
                .apply();
    }

    public LoginFormBean loadLoginInfo() {
        SharedPreferences sharedPreferences = App.getApp().getSharedPreferences("app_szt_xc", Context.MODE_PRIVATE);
        LoginFormBean loginFormBean = new LoginFormBean();
        loginFormBean.setUsername(sharedPreferences.getString("username", ""));
        loginFormBean.setPassword(sharedPreferences.getString("password", ""));
        return loginFormBean;
    }

    public void doLogin(String username, String password, final LoadingCallback<UserInfoBean> callback) {

        ApiClient.getWebService().login(username, password).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ApiResponse<UserInfoBean>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        callback.onStart();
                    }

                    @Override
                    public void onCompleted() {
                        callback.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onError(e.getLocalizedMessage());
                    }

                    @Override
                    public void onNext(ApiResponse<UserInfoBean> userInfoBeanApiResponse) {
                        if (userInfoBeanApiResponse.getCode() == 200) {
                            UserInfoBean data = userInfoBeanApiResponse.getData();
                            userDao.save(data);
                            callback.onSuccess(data);
                        } else {
                            callback.onError("登录失败");
                        }
                    }
                });
    }

    public LiveData<UserInfoBean> getUser() {
        return userDao.load();
    }

    public void refreshUser() {

        ApiClient.getWebService().profile().subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io()).subscribe(new Subscriber<ApiResponse<UserInfoBean>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ApiResponse<UserInfoBean> userInfoBeanApiResponse) {
                int code = userInfoBeanApiResponse.getCode();
                if (code == 200) {
                    UserInfoBean data = userInfoBeanApiResponse.getData();
                    userDao.save(data);
                }
            }
        });
    }
}
